# -*- coding: utf-8 -*-
from abc import ABC

from app.views import CommonHandler
from app.models.dao import db2vectors, delete, add,deleteSmVentilate,db2meta,update
from app.models.model import SMVentilateModel
import app.models.model as model
from datetime import datetime
import app.tools.SafeMonitorTools.m_position_layer_type as smType
meta_id = smType.default_MetaId

class Ventination_Rotation_Handler(CommonHandler, ABC):
    async def get(self):
        meta_id = self.get_argument("meta_id", None)
        ventilations = db2vectors(SMVentilateModel, meta_id)
        await self.respose(data=ventilations)

    async def post(self):
        params = self.json_params()
        ventilation = SMVentilateModel(params)
        add(ventilation)
        ventilations = db2vectors(SMVentilateModel, meta_id)
        # 修改完成后 同时修改数据库中图纸的版本，方便其他客户端进行数据更新  将meta版本 + 1
        meta = db2meta(meta_id)
        metaid = meta['id']
        version_num = meta['mine_name'][1:]
        newVersion = 'v' + str(int(version_num) + 1)
        meta['mine_name'] = newVersion
        # 时间也需要做更新
        meta['update_time'] = datetime.now()
        meta['create_time'] = datetime.fromisoformat(meta['create_time'])
        update(model.MetaModel,metaid,meta)
        await self.respose(data={'type':'success','data':ventilations})


    async def delete(self):
        params = self.json_params()
        print('ventilate delete param',params)
        id = params['node_id']
        delete(SMVentilateModel,id)
        # 删除之后，需要返回客户端的剩余风向线
        ventilations = db2vectors(SMVentilateModel, meta_id)
        # 修改完成后 同时修改数据库中图纸的版本，方便其他客户端进行数据更新  将meta版本 + 1
        meta = db2meta(meta_id)
        metaid = meta['id']
        version_num = meta['mine_name'][1:]
        newVersion = 'v' + str(int(version_num) + 1)
        meta['mine_name'] = newVersion
        # # 时间也需要做更新
        meta['update_time'] = datetime.now()
        meta['create_time'] = datetime.fromisoformat(meta['create_time'])
        update(model.MetaModel,metaid,meta)
        await self.respose(data={'type':'success','data':ventilations})


